<?php

namespace App\Models\HardWareApi;

use Grimzy\LaravelMysqlSpatial\Types\Point;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;

class TrashMessage extends Model
{
    use SoftDeletes;
    use SpatialTrait;
    public $primaryKey = 'id';
    protected $guarded = [];
    protected $spatialFields = ['location'];

    protected $hidden = ['deleted_at','created_at','updated_at'];
    //查询list
    public function getList($request)
    {
    	$per_page = $request->input('per_page',10);
    	return self::orderBy('id','desc')
    				->with('trashSite')
	                ->where(function($query) use ($request){
	                    //检测当前的请求中是否包含park_name、principal参数
	                    $trash_id = $request->input('trash_id', '');//垃圾点位id
	                    $number = $request->input('number', '');//垃圾桶编号
                        $location = $request->input('location','');
	                    if(!empty($number)) {
	                        $query->where('number',$number);
	                    }
	                    if(!empty($trash_id)) {
	                        $query->where('trash_id',$trash_id);
	                    }
                        if(!empty($location)) {
                            $location = Point::fromString($location);
                            $query->distance('location', $location, 10);
                        }
	                })
	                ->paginate($per_page);
    }

    public function trashSite()
    {
    	return $this->belongsTo(\App\Models\OperationManagement\TrashSite::class,'trash_id','id')->select('id','name');
    }
}
